import 'package:flutter/cupertino.dart';
import 'package:rxdart/subjects.dart';
import 'package:schoollifedemo/base/dio_network_util.dart';
import 'package:schoollifedemo/base/routers.dart';
import 'package:schoollifedemo/base/url_contract.dart';
import 'package:schoollifedemo/commonwidget/common_base_viewmodel.dart';
import 'package:schoollifedemo/entity/Entity.dart';

class LoginPageViewModel extends BaseViewModel {
  PublishSubject loginStateSubject = PublishSubject<CommonLoadingState?>();
  final String _accountKey = 'user_account';
  final String _passwordKey = 'user_password';
  final String _autoLoginKey = 'autoLoginKey';

  void turnToRegisterPage(BuildContext context) {
    getApplication().routerNavigateTo(context, RoutersPath.registrationPage);
  }

  void turnToManagerPage(BuildContext context){

  }

  // 转换为SHA256
  Future<String?> _convertSHA256(String password) async {
    return await getChannelService()
        .getCommonChannelFunc()
        ?.convertSHA256(password);
  }

  // 保存是否自动登录
  Future<void> saveIsAutoLoginToSharedPreference(bool isAutoLogin) async {
    getChannelService()
        .getCommonChannelFunc()
        ?.saveToSharedPreferenceBool(_autoLoginKey, isAutoLogin);
  }

  Future<bool?> getIsAutoLoginFromSharedPreference(bool defaultValue) async {
    return await getChannelService()
        .getCommonChannelFunc()
        ?.getFromSharedPreferenceBool(_autoLoginKey, defaultValue);
  }

  // 保存账号密码到本地
  Future<void> saveAccountAndPasswordToSharedPreference(
    String account,
    String password,
  ) async {
    // 先转化为sha256 再进行保存
    String? convertPassword = await _convertSHA256(password);
    if (convertPassword == null) {
      return;
    }
    getChannelService()
        .getCommonChannelFunc()
        ?.saveToSharedPreferenceString(_accountKey, account);
    getChannelService()
        .getCommonChannelFunc()
        ?.saveToSharedPreferenceString(_passwordKey, convertPassword);
  }

  // 从SharedPreference获取账号
  Future<String?> getAccountFromSharedPreference() async {
    return await getChannelService()
        .getCommonChannelFunc()
        ?.getFromSharedPreferenceString(_accountKey, '');
  }

  // 从SharedPreference获取密码
  Future<String?> getPasswordFromSharedPreference() async {
    return await getChannelService()
        .getCommonChannelFunc()
        ?.getFromSharedPreferenceString(_passwordKey, '');
  }

  // 跳转到主页
  Future<void> gotoHomePage(BuildContext buildContext) async {
    // 替换本页面进入
    getApplication().routerNavigateTo(
      buildContext,
      RoutersPath.homePage,
      replace: true,
    );
  }

  // 登录
  Future<bool?> loginAccountWithPassword({
    required String account,
    required String password,
    bool isAutoLogin = false,
  }) async {
    loginStateSubject.add(CommonLoadingState.loading);
    final params = <String, dynamic>{};
    params['account'] = account;
    if (!isAutoLogin) {
      params['password'] = await _convertSHA256(password); // 转换密码为SHA256
    } else {
      params['password'] = password;
    }
    Map<String, dynamic>? result =
        await getNetworkUtil().postHttp(UrlContract.login, params: params);
    if (result == null) {
      loginStateSubject.add(null);
      return null;
    }
    final resp = CommonResponse.fromMap(result);
    if (resp.status == 0) {
      if (resp.trueOrFalse == true) {
        // 登录成功，跳转到主页
        loginStateSubject.add(CommonLoadingState.successTypeTwo);
        return true;
      } else {
        loginStateSubject.add(CommonLoadingState.failure);
        return false;
      }
    } else {
      // 服务器错误
      loginStateSubject.add(null);
      return null;
    }
  }

  void setLoginAccount(String account) {
    getApplication().setLoginAccount(account);
  }
}
